package com.heima.article.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreeMarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojo.ApArticle;
import com.heima.model.search.vos.SearchArticleVo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;

@Service
@Slf4j
@Transactional
public class ArticleFreeMarkerServiceImpl implements ArticleFreeMarkerService {


    @Autowired
    private Configuration configuration;
    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private ApArticleService apArticleService;
    @Override
    @Async
    public void buildArticleMinIo(ApArticle apArticle, String content) {
//
//
//        if (StringUtils.isNotBlank(content)){
//            Template template = null;
//            StringWriter out = new StringWriter();
//
//            try {
//                template = configuration.getTemplate("article.ftl");
//
//                HashMap<String, Object> stringObjectHashMap = new HashMap<>();
//                stringObjectHashMap.put("content", JSONArray.parseArray(content));
//                template.process(stringObjectHashMap,out);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//            InputStream inputStream = new ByteArrayInputStream(out.toString().getBytes());
//            String s = fileStorageService.uploadHtmlFile("", apArticle.getId() + ".html", inputStream);
//
//            apArticleService.update(Wrappers.<ApArticle>lambdaUpdate().eq(ApArticle::getId,apArticle.getId())
//                    .set(ApArticle::getStaticUrl,s));
//        }

        if (StringUtils.isNotBlank(content)){

            StringWriter out = new StringWriter();
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            try {
                Template template = configuration.getTemplate("article.ftl");

                JSONArray objects = JSONArray.parseArray(content);
                stringObjectHashMap.put("content",objects);
                template.process(stringObjectHashMap,out);

            } catch (Exception e) {
                e.printStackTrace();
            }
            InputStream inputStream  = new ByteArrayInputStream(out.toString().getBytes());
            String s = fileStorageService.uploadHtmlFile("", apArticle.getId() + ".html", inputStream);

            apArticleService.update(Wrappers.<ApArticle>lambdaUpdate().eq(ApArticle::getId,apArticle.getId())
                    .set(ApArticle::getStaticUrl,s));

            createArticleIndex(apArticle,content,s);
        }


    }

    @Autowired
    private KafkaTemplate<String,Object> kafkaTemplate;

    private void createArticleIndex(ApArticle apArticle, String content, String s) {

        SearchArticleVo searchArticleVo = new SearchArticleVo();

        searchArticleVo.setContent(content);
        searchArticleVo.setStaticUrl(s);

        kafkaTemplate.send(ArticleConstants.ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(searchArticleVo));


    }
}
